package cn.gemiman.daoyi.module.iot.controller.app.userequipmentplace;

import cn.gemiman.daoyi.framework.common.pojo.CommonResult;
import cn.gemiman.daoyi.framework.common.pojo.PageResult;
import cn.gemiman.daoyi.framework.common.util.object.BeanUtils;
import cn.gemiman.daoyi.framework.security.core.annotations.PreAuthenticated;
import cn.gemiman.daoyi.module.iot.controller.admin.userequipmentplace.vo.UserEquipmentPlacePageReqVO;
import cn.gemiman.daoyi.module.iot.controller.admin.userequipmentplace.vo.UserEquipmentPlaceRespVO;
import cn.gemiman.daoyi.module.iot.controller.admin.userequipmentplace.vo.UserEquipmentPlaceSaveReqVO;
import cn.gemiman.daoyi.module.iot.controller.app.userequipmentplace.vo.AppUserEquipmentPlaceSaveReqVO;
import cn.gemiman.daoyi.module.iot.dal.dataobject.userequipmentplace.UserEquipmentPlaceDO;
import cn.gemiman.daoyi.module.iot.service.userequipmentplace.UserEquipmentPlaceService;
import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

import static cn.gemiman.daoyi.framework.common.pojo.CommonResult.success;
import static cn.gemiman.daoyi.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 APP - 会员设备场所")
@RestController
@RequestMapping("/iot/user-equipment-place")
@Validated
public class AppUserEquipmentPlaceController {

    @Resource
    private UserEquipmentPlaceService userEquipmentPlaceService;

    @PostMapping("/create")
    @Operation(summary = "创建会员设备场所")
    @PreAuthenticated
    public CommonResult<Long> createUserEquipmentPlace(@Valid @RequestBody AppUserEquipmentPlaceSaveReqVO createReqVO) {
        return success(userEquipmentPlaceService.createUserEquipmentPlace(BeanUtil.toBean(createReqVO, UserEquipmentPlaceSaveReqVO.class).setMemberUserId(getLoginUserId())));
    }

    @PutMapping("/update")
    @Operation(summary = "更新会员设备场所")
    @PreAuthenticated
    public CommonResult<Boolean> updateUserEquipmentPlace(@Valid @RequestBody AppUserEquipmentPlaceSaveReqVO updateReqVO) {
        userEquipmentPlaceService.updateUserEquipmentPlace(BeanUtil.toBean(updateReqVO, UserEquipmentPlaceSaveReqVO.class).setMemberUserId(getLoginUserId()));
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除会员设备场所")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthenticated
    public CommonResult<Boolean> deleteUserEquipmentPlace(@RequestParam("id") Long id) {
        userEquipmentPlaceService.deleteUserEquipmentPlace(id, getLoginUserId());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得会员设备场所")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthenticated
    public CommonResult<UserEquipmentPlaceRespVO> getUserEquipmentPlace(@RequestParam("id") Long id) {
        UserEquipmentPlaceDO userEquipmentPlace = userEquipmentPlaceService.getUserEquipmentPlace(id);
        if (!Objects.equals(getLoginUserId(), userEquipmentPlace.getMemberUserId())) {
            userEquipmentPlace = null;
        }
        return success(BeanUtils.toBean(userEquipmentPlace, UserEquipmentPlaceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会员设备场所分页")
    @PreAuthenticated
    public CommonResult<PageResult<UserEquipmentPlaceRespVO>> getUserEquipmentPlacePage(@Valid UserEquipmentPlacePageReqVO pageReqVO) {
        pageReqVO.setMemberUserId(getLoginUserId());
        PageResult<UserEquipmentPlaceDO> pageResult = userEquipmentPlaceService.getUserEquipmentPlacePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserEquipmentPlaceRespVO.class));
    }

}
